//
//  ZMYNewPasswViewController.m
//  奇阅
//
//  Created by 奇阅科技 on 14-10-22.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "ZMYNewPasswViewController.h"
#import "Config.h"
@interface ZMYNewPasswViewController ()

@end

@implementation ZMYNewPasswViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setLeftBackButtonForNavigation];
    [self setTitleForNavigation:@"登录"];
    if (![self.result objectForKey:@"status"]) {
        self.nextButton.enabled = NO;
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.textField resignFirstResponder];
    [self.secondTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)nextClick:(id)sender {
    [self.textField resignFirstResponder];
    [self.secondTextField resignFirstResponder];
    [self.navigationController setSGProgressPercentage:60 andTintColor:RGB_COLOR(255, 114, 0)];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setObject:@"update_userpwd" forKey:@"a"];
    [param setObject:[self.result objectForKey:@"uid"] forKey:@"uid"];
    [param setObject:@"" forKey:@"passwdhash"];
    [param setObject:[self.result objectForKey:@"token"] forKey:@"token"];
    if ([self.result objectForKey:@"status"]) {
        [param setObject:@"reg" forKey:@"type"];
    }
    [[ZMYNetWorkAPI default]smsLoginMobileParam:param response:^(NSDictionary *resultDic) {
        [self.navigationController setSGProgressPercentage:100];
//        [UserAccount removeAccount];
//        [UserAccount saveAccount:[UserAccount getFromDict:resultDic]];
//        [self.navigationController popToRootViewControllerAnimated:NO];
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"toUserController" object:@"login"];
        [[AppDelegate delegate]goToBookRack];
    } error:^(NSString *errorStr) {
        [self.navigationController cancelSGProgress];
        [AppDelegate showwarnHUDAndHide:errorStr view:self.view];
    }];
    
    
}
- (IBAction)loginButton:(id)sender {
    [self.textField resignFirstResponder];
    [self.secondTextField resignFirstResponder];
    if (self.textField.text.length<6||self.secondTextField.text.length<6) {
        [AppDelegate showHUDAndHide:@"密码长度不能低于6位" view:self.view];
        return;
    }
    if ([self.textField.text isEqualToString:self.secondTextField.text]==NO) {
        [AppDelegate showHUDAndHide:@"密码输入不一致，请重新输入" view:self.view];
        return;
    }
    [self.navigationController setSGProgressPercentage:60 andTintColor:RGB_COLOR(255, 114, 0)];
    NSMutableDictionary *param = [NSMutableDictionary new];//@{@"a":@"update_userpwd",@"uid":[self.result objectForKey:@"uid"],@"passwdhash":self.textField.text.md5};
    [param setObject:@"update_userpwd" forKey:@"a"];
    [param setObject:[self.result objectForKey:@"uid"] forKey:@"uid"];
    [param setObject:self.textField.text.md5 forKey:@"passwdhash"];
    [param setObject:[self.result objectForKey:@"token"] forKey:@"token"];
    if ([self.result objectForKey:@"status"]) {
        [param setObject:@"reg" forKey:@"type"];
    }
    [[ZMYNetWorkAPI default]smsLoginMobileParam:param response:^(NSDictionary *resultDic) {
        [self.navigationController setSGProgressPercentage:100];
//        [UserAccount removeAccount];
//        [UserAccount saveAccount:[UserAccount getFromDict:resultDic]];
//        [self.navigationController popToRootViewControllerAnimated:NO];
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"toUserController" object:@"login"];
        [[AppDelegate delegate]goToBookRack];
    } error:^(NSString *errorStr) {
        [self.navigationController cancelSGProgress];
        [AppDelegate showwarnHUDAndHide:errorStr view:self.view];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
